Help

A good resource to receive help is by clicking Help > Markdown Quick Reference. The default linked page is http://rmarkdown.rstudio.com.

Useful Shortcuts:

  • Knit whole document: Ctr + Shift + R (unfortunately doesn’t work all the time)
  • Insert Code Chunk: Ctr + Alt + I

Text and Formatting

Font Types

  • Bold: Use either Stars or _Underscores__
  • Italic: Use either Stars or Underscores
  • Code: Use Single Backticks
  • Superscripts: Use the Power-sign
  • Subscripts: Use the Tilde-sign
  • Footnotes: Use Sqaured Brackets with the power sign 1

Use the arrow sign at the beggining of a line for Blockquotes

Lists & Enumerations

  • List Item No. 1
  • List Item No. 2 Sub List Item Sub Liste Item
  1. Enumeration Item No.1
  2. Second enumeration item
  3. The numbers are incremented automatically

Tables

First Header Second Header
Content Cell Content Cell
Content Cell Content Cell

Page Break

You can end the page by using three or more stars or dashes An html-Document will just print a line (see below)


Integration of code

Chunks

You can embed an R code chunk like this (Shortcut in RStudio: Str + Alt + I):

print("Hello World")
## [1] "Hello World"

It is often useful to give each code chunk a name right after the letter ‘r’. There are a number of arguments that can be executed for each chunk. The whole format is: {r Chunk Name, Optional Argument 1, Optional Argument2}. The most important arguments are:

  • eval = FALSE won’t execute the code within the chunk
  • include = FALSE will execute the code but completely hide any output
  • echo = FALSE will only show the results (graphs and tables) but no code
  • message = FALSE and warning = FALSE will hide messages and warnings (no shit)
  • cache = TRUE will save the output in cache and not re-run unless code within the code is changed (clean cache with knitr::clean_cache)
  • dependson = character_vector will also re-run code in all chunks specified in the character vector as long as this code is re-run (to prevent irregularities caused by a TRUE cache argument in “dependson”-chunks)
  • Full List at https://yihui.name/knitr/options/

In order to apply an option globally (i.e. for all chunks), use the knitr::opts_chunk$set command (ideally in the first setup chunk). The function takes many arguments separated by commas.

Code in Text

You can also integrate R-output within the text. Use `r followed by a space and the corresponding code for that. For example, the number of rows in the mtcars dataset is 32.

Data Frames

By default, data frames will be shown like in the R console:

head(mtcars)
##                    mpg cyl disp  hp drat    wt  qsec vs am gear carb
## Mazda RX4         21.0   6  160 110 3.90 2.620 16.46  0  1    4    4
## Mazda RX4 Wag     21.0   6  160 110 3.90 2.875 17.02  0  1    4    4
## Datsun 710        22.8   4  108  93 3.85 2.320 18.61  1  1    4    1
## Hornet 4 Drive    21.4   6  258 110 3.08 3.215 19.44  1  0    3    1
## Hornet Sportabout 18.7   8  360 175 3.15 3.440 17.02  0  0    3    2
## Valiant           18.1   6  225 105 2.76 3.460 20.22  1  0    3    1

Alternatively, use kable:

knitr::kable(head(mtcars))
mpg cyl disp hp drat wt qsec vs am gear carb
Mazda RX4 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4
Mazda RX4 Wag 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4
Datsun 710 22.8 4 108 93 3.85 2.320 18.61 1 1 4 1
Hornet 4 Drive 21.4 6 258 110 3.08 3.215 19.44 1 0 3 1
Hornet Sportabout 18.7 8 360 175 3.15 3.440 17.02 0 0 3 2
Valiant 18.1 6 225 105 2.76 3.460 20.22 1 0 3 1

For more control consult ?knitr::table or look at thep packages xtable, stargazer, pander, tables or ascii.

Including Plots

You can also embed plots, for example:

Note that the echo = FALSE parameter was added to the code chunk to prevent printing of the R code that generated the plot.

We have data about 53940 diamonds. Only 126 are larger than 2.5 carats. The distribution of the remainder is shown below:

Working Example: Diamonds

There are many exercises to examine the diamonds dataset in Wickhams Data Science for R. The size of a diamond can be plotted against its cut:

This reveals an interesting property. On average, ideally cut diamonds weigh less than diamonds with a worse cut. Note that carat basically measures the weight of a stone. A similar observation can be made with respect to color and clarity:

Note that I1 & J corresond to the worst category respectively.

Diamond sizes

The 20 biggest largest diamonds are:

carat cut color clarity depth table price x y z
5.01 Fair J I1 65.5 59 18018 10.74 10.54 6.98
4.50 Fair J I1 65.8 58 18531 10.23 10.16 6.72
4.13 Fair H I1 64.8 61 17329 10.00 9.85 6.43
4.01 Premium I I1 61.0 61 15223 10.14 10.10 6.17
4.01 Premium J I1 62.5 62 15223 10.02 9.94 6.24
4.00 Very Good I I1 63.3 58 15984 10.01 9.94 6.31
3.67 Premium I I1 62.4 56 16193 9.86 9.81 6.13
3.65 Fair H I1 67.1 53 11668 9.53 9.48 6.38
3.51 Premium J VS2 62.5 59 18701 9.66 9.63 6.03
3.50 Ideal H I1 62.8 57 12587 9.65 9.59 6.03
3.40 Fair D I1 66.8 52 15964 9.42 9.34 6.27
3.24 Premium H I1 62.1 58 12300 9.44 9.40 5.85
3.22 Ideal I I1 62.6 55 12545 9.49 9.42 5.92
3.11 Fair J I1 65.9 57 9823 9.15 9.02 5.98
3.05 Premium E I1 60.9 58 10453 9.26 9.25 5.66
3.04 Very Good I SI2 63.2 59 15354 9.14 9.07 5.75
3.04 Premium I SI2 59.3 60 18559 9.51 9.46 5.62
3.02 Fair I I1 65.2 56 10577 9.11 9.02 5.91
3.01 Premium I I1 62.7 58 8040 9.10 8.97 5.67
3.01 Premium F I1 62.2 56 9925 9.24 9.13 5.73

Naturally, they are characterised by high values for there length (x), width (y) and height (z). Moreover, a lot of them have the clarity “I1” which is the worst. This is in line with the previous findings but still surprising.

Advanced

Parameters

You can set and access parameters in the document header. Here, I set a parameter model: "a4" in the document header. The code below accesses the parameter with params$class to return a plot with the model A4 exclusively.

Another parameter set in the intro let’s you know that generating this document started at 2019-09-24 22:11:56. The Knit dropdown menu supports setting parameters. For even crazier ideas check rmarkdown::render, purrr:pwalk and this example.


  1. You’ll have to numerate them beforehand